﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;
using System.ComponentModel;
using Microsoft.Phone.Net.NetworkInformation;
using System.IO.IsolatedStorage;
namespace KOF.Views
{
    public partial class Events : PhoneApplicationPage
    {
      
        public Events()
        {
            try
            {
                //This Code checks internet Connection
                if (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None)
                {

                }
                else
                {
                    MessageBox.Show("Please check your internet connection!");
                }
               
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            InitializeComponent();


            Visibility darkBackgroundVisibility =
          (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];

            // Write the theme background value.
            if (darkBackgroundVisibility == Visibility.Visible)
            {
                Color newColor = Colors.Black;
                Brush imageColor = new SolidColorBrush(newColor);

                btnSave.Background = imageColor;
               


            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {

                if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
                    return;
           
            string title = txtevent.Text;
            string StartDate = txtto.Value.ToString(); 
            string Description = txtdesc.Text;

             // Get Title , Description ,  Start Date set values to TextBox

            if (NavigationContext.QueryString.TryGetValue("title", out title) && NavigationContext.QueryString.TryGetValue("StartDate", out StartDate) && NavigationContext.QueryString.TryGetValue("Description", out Description))
            {
                txtevent.Text = title;
                txtfrom.Value = Convert.ToDateTime(StartDate);
                txtto.Value = Convert.ToDateTime(StartDate).AddDays(1);
              //  txtfrom.Value = Convert.ToDateTime(DateTime.ParseExact(StartDate, "dd/MM/yyyy", null));
              //  txtto.Value = Convert.ToDateTime(DateTime.ParseExact(StartDate, "dd/MM/yyyy", null)).AddDays(1);
                txtdesc.Text = Description;
            }

          
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            base.OnNavigatedTo(e);
        }

      
        // Save Reminder
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DateTime _Date = txtfrom.Value.Value;

                TimeSpan _Time1 = tpkTime.Value.Value.TimeOfDay;
                TimeSpan _Time = tpkTime.Value.Value.TimeOfDay.Add(_Time1);
                _Time = tpkTime.Value.Value.AddHours(5).TimeOfDay.Add(_Time);
                _Date = _Date.Date + _Time;
                String _Content = txtevent.Text;

                if (String.IsNullOrEmpty(txtdesc.Text))
                    MessageBox.Show("Your task can't be empty !\n Please enter task !");
                else
                {
                    if (_Date < DateTime.Now)
                        _Date = Convert.ToDateTime(txtfrom.Value.ToString());
                    ScheduledAction _OldReminder = ScheduledActionService.Find("TodoReminder"); if (_OldReminder != null)
                        ScheduledActionService.Remove(_OldReminder.Name);
                    Reminder _Reminder = new Reminder("TodoReminder")
                    {
                        BeginTime = _Date,
                        Title = txtevent.Text,
                        Content = _Content,
                    };
                    ScheduledActionService.Add(_Reminder);
                    MessageBox.Show("Release reminder added successfully.");
                }
            }
            catch (FormatException ex)
            {
                MessageBox.Show("Format of date must be dd/mm/yyyy");
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show("The maximum length for title property is 63 characters.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

            }

        }

        //Redirect to Favorite Tab
        private void btnFavorites_Click(object sender, EventArgs e)
        {
            try
            {
            this.NavigationService.Navigate(new Uri("/Views/Favorites.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Redirect to Setting Tab
        private void btnSettings_Click(object sender, EventArgs e)
        {
            try
            {
            this.NavigationService.Navigate(new Uri("/Views/Settings.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Redirect To Release Tab
        private void btnReleases_Click(object sender, EventArgs e)
        {
            try
            {
                IsolatedStorageSettings.ApplicationSettings["loader"] = false;
                this.NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Redirect To News Tab
        private void btnNews_Click(object sender, EventArgs e)
        {
            try
            {
            this.NavigationService.Navigate(new Uri("/Views/News.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        // Go Back
        private void btnBack_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                this.NavigationService.GoBack();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
          
        }
    }
}